<?php
class SoapApiAgent implements IApiService
{
    public $root_url = "https://api2.libertyreserve.com/services/apiservice.asmx?WSDL";
    public $auth;
    public $client;
    
    function __construct(Authentication $a)
    {
        $this->auth = $a;    
    }
   
    public function setupCredentials()
    {        
        $this->client = new SoapClient($this->root_url); 
        
        $Authentication = array('AccountNumber' => $this->auth->accountId, 'ApiName' => $this->auth->apiName, 'Token' => $this->auth->createAuthToken());
        $header =  new SoapHeader("https://api2.libertyreserve.com/soap/", "Authentication", $Authentication);
        $this->client->__setSoapHeaders(array($header));
    }
    
    public function accountName($accountToRetrieve)
    {
        $this->setupCredentials();  
                                                         
        try{
            $accountName = $this->client->AccountName($accountToRetrieve); 
            return $accountName;
        }
        catch (Exception $e)
        {
            $this->outputError($e);
        } 
    }
    
    public function balance($currency)
    {
        $this->setupCredentials(); 
        
        try{
            $balance = $this->client->Balance(); 
            return $balance->$currency;
        }
        catch (Exception $e)
        {
            $this->outputError($e);
        }     
    }
    
    public function history($dateFrom, $dateTo, $currency="Any", $direction="Any", $source="Any", $anonymous="Any", $reference="", $relatedAccount="", $amountFrom=0.0, $amountTo=0.0)
    {
        $dateFrom = $this->getSoapDate($dateFrom);
        $dateTo = $this->getSoapDate($dateTo);
        
        
        $spec = array(
            'From' => $dateFrom, 
            'Till' => $dateTo, 
            'Currency' => $currency, 
            'Direction' => $direction, 
            'RelatedAccount' => $relatedAccount, 
            'Reference' => $reference, 
            'Source' => $source, 
            'Private' => $anonymous, 
            'AmountFrom' => $amountFrom, 
            'AmountTo' => $amountTo 
        );
        
        $this->setupCredentials(); 
        
        try{
            $pageNumber = 0;
            $history = array();
            while(true)
            {
                $historyPage = $this->client->History($spec, $pageNumber++, 20);   
                if(is_array($historyPage->Transactions->Transaction))
                    $history = array_merge($history, $historyPage->Transactions->Transaction); 
                else
                    array_push($history, $historyPage->Transactions->Transaction);
                
                if(!$this->hasMorePages($historyPage))
                { 
                    foreach($history AS $item)
                    {
                        $item->Date = $this->getFormatDate($item->Date);
                    }
                    return $history;                 
                }     
            };
        }
        catch (Exception $e)
        {
            $this->outputError($e);
        }           
    }
    
    public function findTransaction($receiptId)
    {
        $this->setupCredentials(); 
        
        try{
            $findTransfer = $this->client->FindTransaction($receiptId); 
            $findTransfer->Date = $this->getFormatDate($findTransfer->Date);
            return $findTransfer;
        }
        catch (Exception $e)
        {
            $this->outputError($e);
        } 
    }
    
    public function transfer($payee, $currency, $amount, $private, $purpose, $reference="", $memo="")
    {        
        $private=="true"?$private=true:$private=false;
        
        $spec = array(
                'Payee' => $payee,
                'Amount' => $amount,
                'Currency' => $currency,
                'Memo' => $memo,
                'Reference' => $reference,
                'Private' => $private,
                'Purpose' => $purpose
        );
        $this->setupCredentials(); 
        
        try { 
            $history = $this->client->Transfer($spec); 
            $history->Date = $this->getFormatDate($history->Date);
            return $history;
        }
        catch (Exception $e)
        {
            $this->outputError($e);
        } 
    }

    private function hasMorePages($response)
    {
        try {
            return (bool)$response->HasMore;
        }
                
        catch (Exception $e) {
            $this->outputError($e);
        }
    }
    
    private function outputError($e)
    {          
        echo $e->getMessage();
        die;
    }
    
     private function getSoapDate($inputDate)
     {
        $timeStamp = strtotime($inputDate);
        $soapDate = date("Y-m-d", $timeStamp). "T" . date("H:i:s", $timeStamp);

        return $soapDate;
     }
     
     private function getFormatDate($inputDate)
     {
        $date = str_replace("T", " ", $inputDate);
        $date = str_replace("Z", "", $date);
        return $date;
     }
}
?>